package com.young.ums.cloud.controller;

import com.young.interfaces.ums.model.*;
import com.young.interfaces.ums.service.IUmsOutService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * ums对外api
 * 对ums包实现的cloud包装
 * Created by Administrator on 2019/12/25 0025.
 */
@RestController
@RequestMapping(value = "/ums-api", method = {RequestMethod.POST, RequestMethod.GET})
public class UmsOutServiceApiController implements IUmsOutService {
    /**
     * 对外服务实现
     * ums包提供的真正实现
     */
    @Resource(name = "umsOutService")
    IUmsOutService umsOutService;
    /**
     * 根据用户ID获取用户包含的资源列表
     *
     * @param userId 用户id
     * @return
     * @throws Exception
     */
    @RequestMapping("/getNavigateByUserId")
    @Override
    public List<Navigate> getNavigateByUserId(String userId) {
        return umsOutService.getNavigateByUserId(userId);
    }

    /**
     * 根据用户id获取资源树(获取菜单),暂未实现
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getNavigateAsTreeByUserId")
    @Override
    public List<Navigate> getNavigateAsTreeByUserId(String userId) {
        return umsOutService.getNavigateAsTreeByUserId(userId);
    }

    /**
     * 根据用户id获取用户登录信息
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getLoginUserById")
    @Override
    public User getLoginUserById(String userId) {
        return umsOutService.getLoginUserById(userId);
    }

    /**
     * 验证单点,即验证token合法性
     *
     * @param token 令牌
     * @return
     * @throws Exception
     */
    @RequestMapping("/verifySSO")
    @Override
    public SsoVerifyDTO verifySSO(String token) {
        return umsOutService.verifySSO(token);
    }

    /**
     * 查询用户上一次登录的时间
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getLastLoginTime")
    @Override
    public Date getLastLoginTime(String userId) {
        return umsOutService.getLastLoginTime(userId);
    }

    /**
     * 根据组织id获取组织信息
     *
     * @param deptId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getDeptById")
    @Override
    public Dept getDeptById(String deptId) {
        return umsOutService.getDeptById(deptId);
    }

    /**
     * 根据角色id获取角色信息
     *
     * @param roleId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getRoleById")
    @Override
    public Role getRoleById(String roleId) {
        return umsOutService.getRoleById(roleId);
    }

    /**
     * 判断是否存在指定id/account的用户
     * 在创建用户之前做唯一性验证
     *
     * @param userIdOrAccount 用户id或者登陆账号
     * @return
     * @throws Exception
     */
    @RequestMapping("/existUserIdOrAccount")
    @Override
    public boolean existUserIdOrAccount(String userIdOrAccount) {
        return umsOutService.existUserIdOrAccount(userIdOrAccount);
    }

    /**
     * 创建用户,供外部调用,id/account/companyId不允许为空
     *
     * @param user
     * @return
     * @throws Exception
     */
    @RequestMapping("/insertUser")
    @Override
    public boolean insertUser(User user) {
        return umsOutService.insertUser(user);
    }

    /**
     * 更新用户信息
     *
     * @param user
     * @return int 改动的数据条数,-1表示操作失败
     * @throws Exception
     */
    @RequestMapping("/updateUser")
    @Override
    public int updateUser(User user) {
        return umsOutService.updateUser(user);
    }

    /**
     * 删除用户
     *
     * @param userId 用户id
     * @return int 改动的数据条数,-1表示操作失败
     * @throws Exception
     */
    @RequestMapping("/deleteUser")
    @Override
    public int deleteUser(String userId) {
        return umsOutService.deleteUser(userId);
    }

    /**
     * 校验用户是否有权限访问uri
     *
     * @param userId 用户id
     * @param uri    uri
     * @return
     */
    @RequestMapping("/canAccessUri")
    @Override
    public boolean canAccessUri(String userId, String uri) {
        return umsOutService.canAccessUri(userId, uri);
    }
}
